import {getRoutersSuccessAction, getUserInfoSuccessAction} from "@/stores/actions/userInfoActions";
import {message} from "antd";
import {convertCompatRouters, getRouters} from "@/services/router";
import {put, takeEvery} from "redux-saga/effects";
import {getInfo} from "@/services/login";
import {GETROUTERS, GETUSERINFO} from "@/stores/actionTypes/userInfoActionType";


function* handleGetRouters() {
    try {
        const res = yield getRouters()
        const routerList = convertCompatRouters(res.data)
        yield put(getRoutersSuccessAction(routerList))
    } catch (error) {
        message.error(error)
    }
}

function* handleGerUserInfo() {
    try {
        const res = yield getInfo()
        yield put(getUserInfoSuccessAction({permissions: res.permissions, roles: res.roles, user: res.user}))
        yield put({type: GETROUTERS})
    } catch (e) {
        message.error(e)
    }
}

export function* userInfoSaga() {
    yield takeEvery(GETUSERINFO, handleGerUserInfo)
    yield takeEvery(GETROUTERS, handleGetRouters)
}